import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class TestDemo {

    //StringBuilder -> String, 调用toString方法
    public static String func2() {
        StringBuilder sb = new StringBuilder();
        return sb.toString();
    }

    //String -> StringBuffer
    public static StringBuffer func() {
        String str = "abcdef";
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        return sb;
        //return new StringBuffer(str);
    }

    //append
    public static void main15(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("abcdef").append("123456");
        System.out.println(sb.toString());
    }

    //isEmpty
    public static void main14(String[] args) {
        String str = "";
        String ret = str.concat("bit");
        System.out.println(ret);
        System.out.println(str.length());
        int[] array = {1,2,3,4,5};
        System.out.println(array.length);//Java 里面的数字 非常特殊 机制很多
        System.out.println(str.isEmpty());
    }

    //toLowerCase
    public static void main13(String[] args) {
        String str = "abcdefBFRG123高";
        String ret = str.toLowerCase();
        System.out.println(ret);
    }

    //trim
    public static void main12(String[] args) {
        String str = "    abc     defg     ";
        String ret = str.trim();
        System.out.print(ret);
        System.out.println("==============");
    }

    //substring
    public static void main11(String[] args) {
        String str = "abcdefgh";
        String sub = str.substring(2,4);//提取子串
        System.out.println(sub);
    }

    //spilt内多个条件用‘|’
    public static void main10(String[] args) {
        String str = "Java30 12&21#hello";
        String[] strings = str.split(" |&|#");
        for (String s:strings) {
            System.out.println(s);
        }
    }

    //特殊的split符号，比如"."
    public static void main9(String[] args) {
        String str = "192.168.1.1";
        String[] strings = str.split("\\.",7);
        for (String s:strings) {
            System.out.println(s);
        }
        System.out.println("=================");
        String str2 = "192\\168\\1\\1";
        strings = str2.split("\\\\",7);
        for (String s:strings) {
            System.out.println(s);
        }

    }

    //split
    public static void main8(String[] args) {
        String str = "name=zhangsan&age=19";
        String[] strings = str.split("&");
        for (String s:strings) {
            //s:  name=zhangsan   下次：s :    age=19
            //System.out.println(s);
            String[] ss = s.split("=");//ss:name  zhangsan
            for (String tmp :ss) {
                System.out.println(tmp);
            }
        }
    }

    //replace
    public static void main7(String[] args) {
        String str = "ababcabcdabcde";
        String ret = str.replaceFirst("ab","pp");
        System.out.println(ret);
    }

    //startwith
    public static void main6(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";

        System.out.println(str.lastIndexOf(tmp,4));
        System.out.println(str.startsWith("c",4));

        System.out.println(str.endsWith("cde"));
    }

    //contains、indexOf
    public static void main5(String[] args) {
        String str = "ababcabcd";
        String tmp = "abc";
        boolean flg = str.contains(tmp);
        System.out.println(flg);
        System.out.println("==============");
        int index = str.indexOf(tmp,3);//类似于C的strstr->  KMP算法
        System.out.println(index);
    }

    //compareto
    public static void main4(String[] args) {
        String str1 = "Abc";
        String str2 = "abc";
        int ret = str1.compareTo(str2);
        System.out.println(ret);
        System.out.println(str1.equalsIgnoreCase(str2));
    }

    //字节转字符串
    public static void main3(String[] args) throws UnsupportedEncodingException {
        byte[] bytes = {97,98,99,100};
        String str = new String(bytes,1,3);
        System.out.println(str);
        System.out.println("===============");
        String str2 = "高博";
        byte[] bytes1 = str2.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
    }

    //isDigit
    public static boolean isNumberChar(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //判断某个字符是不是数字
            boolean flg = Character.isDigit(c);
            if(flg == false) {
                return false;
            }
            /*if(c < '0' || c > '9') {
                return false;
            }*/
        }
        return true;
    }

    //isNumberChar
    public static void main2(String[] args) {
        String str = "124567";
        System.out.println(isNumberChar(str));
    }

    //String构造方法、charAT、toCharArray
    public static void main1(String[] args) {
        char[] val = {'a','b','c','d','e'};
        String str = new String(val,1,3);
        System.out.println(str);

        System.out.println("===========");
        String str2 = "hello";
        char ch = str2.charAt(2);//获取到2下标的字符
        System.out.println(ch);

        char[] chars = str2.toCharArray();//把str2指向的字符串对象，变成字符数组
        System.out.println(Arrays.toString(chars));
    }

}
